﻿/**
 * pointsChart
 * Charts Collection
 *
 * @author		Duarte Peixinho
 * @version		1.0
 */

package com.duartepeixinho.charts.chartsType
{
	import com.duartepeixinho.charts.grid.ChartGrid
	import flash.display.Shape;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class PointsChart extends ChartGrid
	{
		//const
		private const POINTS_DEFAULT_RADIUS:uint = 3;
		
		//vars
		private var _values:Array;		
		
		/**
		 * Creates a Point Chart.
		 * 
		 * @param		values			Array		Schema of the array: [ x:Number, y:Number, radius:uint, color:uint, label:String ]
		 * @param		coordEnd		Point		Point of the width and heigth if the axis
		 * @param		CountInit		Point		Point that determines the starting number of each axis
 		 * @param		CountEnd		Point		How many numbers in each axis
		 * @param		xCount			Number		Increase Number of the XX axis
		 * @param		yCount			Number		Increase Number of the YY axis
		 * @param		showXMarks		Boolean		Show Labels of the XX axis
		 * @param		showYMarks		Boolean		Show Labels of the YY axis
		 * @param		xLabel			String		String to appear as the label of the XX axis
		 * @param		yLabel			String		String to appear as the label of the YY axis
		 * @param		grid			Boolean		Show the background grid or not
		 * @param		showZero		Boolean		Show the 0 on the corner or not
		 * @param		title			String		Title of the chart
		 */
		
		public function PointsChart(values:Array, coordEnd:Point, CountInit:Point, CountEnd:Point, xCount:Number, yCount:Number, showXMarks:Boolean, showYMarks:Boolean, xLabel:String = null, yLabel:String = null, grid:Boolean = true, showZero:Boolean = false, title:String = null )
		{		
			//instantiates the parent constructor
			super(coordEnd, CountInit, CountEnd, xCount, yCount, showXMarks, showYMarks, xLabel, yLabel, grid, showZero, title);
			_values = values;
			
			//draw the points in the chart
			buildPoints();
		}		
		
		private function buildPoints():void {
			
			//sets the var color
			var pointColor:uint;
			
			//getting colors automatically
			var colorsPallette:Number = 10777215;
			//convert dec to hex: (16777215).toString(16);
			
			//loop to get/set the color
			for (var i:int = 0; i < _values.length; i++) {
				if (_values[i][3].length == 0) {
					//sets the color
					_values[i][3] = "0x" + (colorsPallette / _values.length * (i+1)).toString(16);
				}
			}
			
			//loop to draw the points
			for (i = 0; i < _values.length; i++) {
				//gets the previous set color
				pointColor = _values[i][3];
				
				//sets the shape
				var shape:Shape = new Shape();
				//starts drawing
				shape.graphics.beginFill(pointColor);
				//converts the location of the point to the chart coordinates using the public function of the parent
				var pointLocation:Point = convertCoordChart(new Point(_values[i][0], _values[i][1]));
				
				if (_values[i][2]) shape.graphics.drawCircle(pointLocation.x, pointLocation.y, _values[i][2]);
				else shape.graphics.drawCircle(pointLocation.x, pointLocation.y, POINTS_DEFAULT_RADIUS);
				shape.graphics.endFill();
				
				//setting the point label if exists in the array
				if (_values[i][4]) {
					//setting the label
					var label:TextField = new TextField();					
					label.text = _values[i][4];
					label.setTextFormat(new TextFormat(null, 9, pointColor));
					label.height = label.textHeight + 5;
					label.y = pointLocation.y -25;
					label.x = pointLocation.x - label.textWidth/2;
					addChild(label);
				}
				addChild(shape);
			}
		}
	}
	
}